<?php
class PacienteController extends Zend_Controller_Action {
	public function init(){
		$this->initView();
		$this->view->title = 'Cadastro de Pacientes';
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$menu = '<ul>
				<li class="slogan"></li>
				<li><a href="'.$this->view->baseUrl.'/user" accesskey="p">Users</li>
				<li class="active">Pacientes</li>
				<li><a href="'.$this->view->baseUrl.'/medicamento" accesskey="p">Medicamentos</a></li>
				<li><a href="'.$this->view->baseUrl.'/os" accesskey="o">OS</a></li>
				<li><a href="'.$this->view->baseUrl.'/carrinho" accesskey="m">Carrinho</a></li>
				<li><a href="'.$this->view->baseUrl.'/entrada" accesskey="m">Entradas/Estoque</a></li>
				</ul>';
		$this->view->menu = $menu;
	
		Zend_Loader::loadClass('Paciente');  // Carrega o model
		$this->view->paciente = Zend_Auth::getInstance()->getIdentity();
	}
	public function indexAction(){
		$pacientes = new Paciente();
		$this->view->pacientes = $pacientes->fetchAll($pacientes->select()->order("id"));
		//$moreHead = '<link rel="stylesheet" href="'.$this->_request->getBaseUrl().'/../public/scripts/js/suite/dhtml-suite-for-applications/demos/css/demos.css" media="screen" type="text/css">'."\n";
		$moreHead = '<script type="text/javascript" src="'.$this->_request->getBaseUrl().'/../public/scripts/js/suite/dhtml-suite-for-applications/js/dhtml-suite-for-applications.js"></script>'."\n";
		$moreHead .= '<script type="text/javascript" src="'.$this->_request->getBaseUrl().'/../public/scripts/js/suite/dhtml-suite-for-applications/js/ajax.js"></script>';
		$this->view->moreHead = $moreHead;
		$this->render();
	}
	public function adicionarAction(){
		$moreHead  = '<link type="text/css" rel="stylesheet" href="'.$this->_request->getBaseUrl().'/../public/scripts/js/calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.css" media="screen"></LINK>'."\n";
		$moreHead .= '<SCRIPT type="text/javascript" src="'.$this->_request->getBaseUrl().'/../public/scripts/js/calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.js"></script>'."\n";
		$this->view->moreHead = $moreHead;
		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter = new Zend_Filter_StripTags();
			$id = $filter->filter($this->_request->getPost('id'));
			$nome = $filter->filter($this->_request->getPost('nome'));
			$fone = $filter->filter($this->_request->getPost('fone'));
			$uf = $filter->filter($this->_request->getPost('uf'));
			$data_nasc = $filter->filter($this->_request->getPost('data_nasc'));
			$n_sus = $filter->filter($this->_request->getPost('n_sus'));
			$cpf = $filter->filter($this->_request->getPost('cpf'));
			$rg = $filter->filter($this->_request->getPost('rg'));
			$endereco = $filter->filter($this->_request->getPost('endereco'));
			$bairro = $filter->filter($this->_request->getPost('bairro'));
			$cidade = $filter->filter($this->_request->getPost('cidade'));

			$exp = explode('/',$data_nasc);
			$dateTime = new DateTime($exp[1].'/'.$exp[0].'/'.$exp[2]);
			$data_nasc = $dateTime->format('m/d/Y');
			
			if ($id != '' && $nome != '') {
				$data = array(
					'id' => $id,
					'nome' => $nome,
					'fone' => $fone,
					'uf' => $uf,
					'data_nasc' => $data_nasc,
					'n_sus' => $n_sus,
					'cpf' => $cpf,
					'rg' => $rg,
					'endereco' => $endereco,
					'bairro' => $bairro,
					'cidade' => $cidade						
				);
				$pacientes = new Paciente();
				$pacientes->insert($data);
				$this->_redirect('/paciente');
				return;
			}
		}
		$pacientes = new Paciente();
		$idNew = $pacientes->getNextId();
		
		$this->view->pacientes = new stdClass();
		$this->view->pacientes->id = $idNew;
		$this->view->pacientes->nome = '';
		$this->view->pacientes->fone = '';
		$this->view->pacientes->uf = '';
		$this->view->pacientes->data_nasc = '';
		$this->view->pacientes->n_sus = '';
		$this->view->pacientes->cpf = '';
		$this->view->pacientes->rg = '';
		$this->view->pacientes->endereco = '';
		$this->view->pacientes->bairro = '';
		$this->view->pacientes->cidade = '';
		
		$this->view->action = 'adicionar';
		$this->view->buttonText = 'Adicionar';
		$this->render();
	}
	public function editarAction()	{
		$this->view->title = "Editar Paciente";
		$moreHead  = '<link type="text/css" rel="stylesheet" href="'.$this->_request->getBaseUrl().'/../public/scripts/js/calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.css" media="screen"></LINK>'."\n";
		$moreHead .= '<SCRIPT type="text/javascript" src="'.$this->_request->getBaseUrl().'/../public/scripts/js/calendar/dhtmlgoodies_calendar/dhtmlgoodies_calendar.js"></script>'."\n";
		$this->view->moreHead = $moreHead;

		$pacientes = new Paciente();
		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter = new Zend_Filter_StripTags();
			$id = (int)$filter->filter($this->_request->getPost('id'));
			$nome = $filter->filter($this->_request->getPost('nome'));
			$fone = $filter->filter($this->_request->getPost('fone'));
			$uf = $filter->filter($this->_request->getPost('uf'));
			$data_nasc = $filter->filter($this->_request->getPost('data_nasc'));
			$n_sus = $filter->filter($this->_request->getPost('n_sus'));
			$cpf = $filter->filter($this->_request->getPost('cpf'));
			$rg = $filter->filter($this->_request->getPost('rg'));
			$endereco = $filter->filter($this->_request->getPost('endereco'));
			$bairro = $filter->filter($this->_request->getPost('bairro'));
			$cidade = $filter->filter($this->_request->getPost('cidade'));

			$exp = explode('/',$data_nasc);
			$dateTime = new DateTime($exp[1].'/'.$exp[0].'/'.$exp[2]);
			$data_nasc = $dateTime->format('m/d/Y');
			
			if ($id !== false) {
				if ($id != '' && $nome != '') {
					$data = array(
						'id' => $id,
						'nome' => $nome,
						'fone' => $fone,
						'uf' => $uf,
						'data_nasc' => $data_nasc,
						'n_sus' => $n_sus,
						'cpf' => $cpf,
						'rg' => $rg,
						'endereco' => $endereco,
						'bairro' => $bairro,
						'cidade' => $cidade						
					);
					$where = 'id = ' . $id;
					$pacientes->update($data, $where);
					$this->_redirect('/paciente');
					return;
				} else {
					$this->view->pacientes = $pacientes->fetchRow('id='.$id);
				}
			}
		} else {
			$id = (int)$this->_request->getParam('id', 0);
			if ($id > 0) {
				$this->view->pacientes = $pacientes->fetchRow('id='.$id);
				//$this->view->pacientes = $pacientes->fetchAll("SELECT to_char(c.data_nasc,'dd/mm/yyyy') as dataNasc FROM clientes c WHERE id=5".$id);
			}
		}
		$this->view->action = 'editar';
		$this->view->buttonText = 'Atualizar';
		$this->render();
	}
	
	public function excluirAction(){
		$this->view->title = "Excluir Paciente";
		$pacientes = new Paciente();
		if ($this->_request->isPost()) {
			Zend_Loader::loadClass('Zend_Filter_Alpha');
			$filter = new Zend_Filter_Alpha();
			$id = (int)$this->_request->getPost('id');
			$del = $filter->filter($this->_request->getPost('del'));
			if ($del == 'Sim' && $id > 0) {
				$where = 'id = ' . $id;
				$rows_affected = $pacientes->delete($where);
			}
		} else {
			$id = (int)$this->_request->getParam('id');
			if ($id > 0) {
				$this->view->pacientes = $pacientes->fetchRow('id='.$id);
				if ($this->view->pacientes->id > 0) {
					$this->render();
					return;
				}
			}
		}
		$this->_redirect('/paciente');
	}
/*	public function buscarAction(){
		$this->view->title = "Busca de Paciente";
		$obj = new Paciente();
		if ($this->_request->isPost()) {
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter = new Zend_Filter_StripTags();
			$search = $filter->filter($this->_request->getPost('search'));
			$data = array('search' => $search);
			$order = 'nome';
			$this->view->pacientes = $obj->fetchAll('upper(nome) like upper(\'%'.$search.'%\')', $order);
		}
	}*/
	public function listarAction(){
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter = new Zend_Filter_StripTags();
		$sortBy = $this->_request->getParam('sortBy');
		$sortAscending = $this->_request->getParam('sortAscending');
		$startIndex = $this->_request->getParam('startIndex');
		$numberOfRows = $this->_request->getParam('numberOfRows');
		$search = $this->_request->getParam('filtro');
		if(isset($sortBy)){
			/*$sqlAdd = "";
			if(isset($sortAscending)){
				if($sortAscending=='false')
					$sqlAdd.=" desc";	
			}
			
			if(isset($startIndex) && $startIndex){
				$sqlAdd .= $startIndex;	
				if(isset($numberOfRows))
					$sqlAdd .= ",".$numberOfRows;

			}elseif(isset($numberOfRows) && $numberOfRows)
				$sqlAdd .= $numberOfRows;*/

			$obj = new Paciente();

			if(empty($search)){
				$this->view->pacientes = $obj->fetchAll($obj->select()->order("id")->limit($numberOfRows,$startIndex));
			}else{
				$order = 'nome';
				$this->view->pacientes = $obj->fetchAll('upper(nome) like upper(\'%'.$search.'%\')', $order,$numberOfRows,$startIndex);
			}
		}
	}
	function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if (!$auth->hasIdentity()) {
			$this->_redirect('auth/login');
		}
	}
}
?>